
import java.util.Random;

/**
 * Iscas 85 Node Class.
 * This is an abstract class that represents a node in the circuit graph.
 * This class is responsible for storing the node information and simulate errors.
 *
 */
public abstract class Iscas85Node {
	/**
	 * Node type
	 */
	public NetlistParser.GateType nodeType;
	/**
	 * Stuck at 0
	 */
	public boolean sa0;
	/**
	 * Stuck at 1
	 */
	public boolean sa1;
	/**
	 * Node value
	 */
	public byte value;
	/**
	 * Node name
	 */
	public String name;
	/**
	 * Node identifier (number)
	 */
	public int identifier;
	/**
	 * Number of fanout connections
	 */
	public int fanoutCount = 0;
	/**
	 * Node state
	 */
	public boolean active = false;

	/**
	 * Processes and propagates the node results
	 * 
	 * @return Result of the node processing
	 */
	public abstract byte processInput();

	/**
	 * Simulates an error, with the given probability
	 * 
	 * @param prob
	 *            Probability than an error can occur
	 */
	public void simulateError(double prob) {
		Random rand = new Random();
		if (rand.nextDouble() <= prob) {
			if(sa0 && sa1)
				value = (byte) (rand.nextInt(2));
			else if(sa0)
				value = 0;
			else if(sa1)
				value = 1;
		}
	}
}
